﻿using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;

namespace Configuration
{
    class PluginController
    {
        public enum INSTALLSTATE : uint
        {
            INSTALLSTATE_NOTUSED,
            INSTALLSTATE_ABSENT,
            INSTALLSTATE_INVALIDARG,
            INSTALLSTATE_LOCAL,
            INSTALLSTATE_SOURCE,
            INSTALLSTATE_SOURCEABSENT,
            INSTALLSTATE_UNKNOWN
        }

        internal static class MsiInterop
        {
            [DllImport("msi", CharSet = CharSet.Auto)]
            public static extern INSTALLSTATE MsiGetComponentPath(string szProduct, string szComponent, ulong lpPathBuf, ulong pcchBuf);
            [DllImport("msi", CharSet = CharSet.Auto)]
            public static extern int MsiGetProductCode(string component, StringBuilder buffer);
        }
        public delegate void MessageEventHandler(object sender, MessageEventArgs e);
        public class MessageEventArgs : EventArgs
        {
            private string _message;
            public MessageEventArgs(string message)
            {
                _message = message;
            }
            public string Message
            {
                get { return this._message; }
            }
        }
        public static class Install
        {
            public static event MessageEventHandler MessageEvent;

            public static bool InstallPIA()
            {
                try
                {
                    OnMessageEvent("Starting MSHTML PIA Install");
                    System.Diagnostics.Process p = new System.Diagnostics.Process();
                    p.StartInfo.UseShellExecute = false;
                    p.StartInfo.FileName = "vs_piaredist.exe";
                    p.StartInfo.CreateNoWindow = false;
                    p.Start();
                    p.WaitForExit();
                    OnMessageEvent("Installation Success");
                    return true;
                }
                catch (Exception e)
                {
                    OnMessageEvent("Installation Failed: " + e.Message);
                    return false;
                }
            }

            public static bool InstallOfficePIA()
            {
                try
                {
                    OnMessageEvent("Starting Office 2007 PIA Install");
                    System.Diagnostics.Process p = new System.Diagnostics.Process();
                    p.StartInfo.UseShellExecute = false;
                    p.StartInfo.FileName = "msiexec.exe";
                    p.StartInfo.Arguments = "/passive /i o2007pia.msi";
                    p.StartInfo.CreateNoWindow = false;
                    p.Start();
                    p.WaitForExit();
                    OnMessageEvent("Installation Success");
                    return true;
                }
                catch (Exception e)
                {
                    OnMessageEvent("Installation Failed: " + e.Message);
                    return false;
                }
            }

            private static void OnMessageEvent(string message)
            {
                if (MessageEvent != null)
                    MessageEvent(null, new MessageEventArgs(message));
            }
        }

        public static bool CheckLibAvailability(string name, string version, string culture, string token)
        {
            string assemblyFormat = "{0}, Version={1}, Culture={2}, PublicKeyToken={3}";
            try
            {
                string assemblyString = string.Format(assemblyFormat, name, version, culture, token);
                Assembly assm = Assembly.Load(assemblyString);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool CheckAppAvailability(string componentCode)
        {
            try
            {
                StringBuilder sb = new StringBuilder(512);
                int result = MsiInterop.MsiGetProductCode(componentCode, sb);
                ulong lpPath = 0;
                INSTALLSTATE state = MsiInterop.MsiGetComponentPath(sb.ToString(), componentCode, lpPath, 257);
                if (state == INSTALLSTATE.INSTALLSTATE_LOCAL)
                    return true;
                else
                    return false;
            }
            catch
            {
                return false;
            }
        }
    }
}

